package 插入排序;

import java.util.Arrays;

public class InsertSort {
    public static int[] insertSort(int[] arr) {
        int n = arr.length;
        for (int i = 1; i < n; i++) {
            int temp = arr[i];
            int k = i - 1;
            while (k >= 0 && arr[k] > temp) {
                k--;
            }
            //腾出位置
            for (int j = i; j > k + 1; j--) {
                arr[j] = arr[j-1];
            }
            arr[k+1] = temp;
        }
        return arr;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(insertSort(new int[]{1,2,5,77,3,14,82,2,4})));
    }
}
